package hc.bre.runner

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.condition.*
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.mongodb.MongoDatabaseFactory

import hc.bre.log.LogStore
import hc.bre.log.MongoLogStore

@groovy.transform.CompileStatic
@Configuration
class RunnerConfiguration {
	protected static final Logger _log = LoggerFactory.getLogger(RunnerConfiguration);
	
	@Bean
	@ConditionalOnClass(MongoDatabaseFactory)
	@ConditionalOnProperty(name = "rule.log.mongo.auto", havingValue = "on",matchIfMissing = true)
	public LogStore logStore(MongoDatabaseFactory mongoFactory) {
		//prop in application.properties,eg: spring.data.mongodb.uri=mongodb://jmserver:27017/ruledb
		LogStore.logStore = new MongoLogStore(mongoFactory);
		_log.info('mongodb rule log storage is started up.{}',mongoFactory)
		return LogStore.logStore;
	}

	@Bean
	@ConditionalOnProperty(name = "rule.runner.auto", havingValue = "on",matchIfMissing = true)
	public ScriptEngineRunner scriptEngineerRunner(@Value('${rule.dir}')String repodir) {
		_log.info('ScriptEngineRunner is registered as spring bean.')
		return new ScriptEngineRunner(repodir);
	}
}
